package com.ndp.fb.rdb.mongo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.*;
import com.ndp.fb.constant.MongoConstants;
import com.ndp.fb.mongo.api.AdImageMongoService;
import com.ndp.fb.mongo.model.Image;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AdImageMongoServiceImpl implements AdImageMongoService {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private DB mongoDBClient;

    @Override
    public void upsertAdImage(Image image) {
        if (image != null) {
            DBCollection dbCollection = mongoDBClient.getCollection(MongoConstants.TABLE_NAME_ADIMAGE);
            DBObject dbObject = (DBObject) com.mongodb.util.JSON.parse(JSONObject.toJSONString(image));
            DBObject updateData = new BasicDBObject();
            updateData.put("$set", dbObject);
            Date dateUpdateTime = new Date();
            dbObject.put("dataUpdateTime", dateUpdateTime);
            dbCollection.update(new BasicDBObject("md5", image.getMd5()), updateData, true, false);
        }
    }

    @Override
    public Image findOne(String md5) {
        if (StringUtils.isEmpty(md5)) {
            return null;
        }
        DBCollection dbCollection = mongoDBClient.getCollection(MongoConstants.TABLE_NAME_ADIMAGE);
        DBObject dbObject = dbCollection.findOne(new BasicDBObject("md5", md5));
        if (dbObject == null) {
            return null;
        }
        return JSONObject.parseObject(JSONObject.toJSONString(dbObject), Image.class);
    }

    @Override
    public List<Image> findHashIsNull() {
        DBCollection dbCollection = mongoDBClient.getCollection(MongoConstants.TABLE_NAME_ADIMAGE);
        DBCursor dbCursor = dbCollection.find(new BasicDBObject("hash", null));
        if (dbCursor == null) {
            return null;
        }
        List<Image> result = new ArrayList<>();
        while (dbCursor.hasNext()) {
            DBObject next = dbCursor.next();
            if (next != null) {
                Image image = JSON.parseObject(JSON.toJSONString(next), Image.class);
                result.add(image);
            } else {
                logger.error("查询有空结果。");
            }
        }
        return result;
    }

	@Override
	public Image findOneByHash(String hash) {
		if (StringUtils.isEmpty(hash)) {
            return null;
        }
        DBCollection dbCollection = mongoDBClient.getCollection(MongoConstants.TABLE_NAME_ADIMAGE);
        DBObject dbObject = dbCollection.findOne(new BasicDBObject("hash", hash));
        if (dbObject == null) {
            return null;
        }
        return JSONObject.parseObject(JSONObject.toJSONString(dbObject), Image.class);
	}

	@Override
	public void updateAdImageUrlByHash(String hash, String url) {
		 DBCollection dbCollection = mongoDBClient.getCollection(MongoConstants.TABLE_NAME_ADIMAGE);
         DBObject dbObject = new BasicDBObject("url", url);
         DBObject updateData = new BasicDBObject();
         updateData.put("$set", dbObject);
         Date dateUpdateTime = new Date();
         dbObject.put("dataUpdateTime", dateUpdateTime);
         dbCollection.update(new BasicDBObject("hash", hash), updateData, true, false);
	}

	@Override
	public void updateAdImageUrlByMd5(String md5, String url) {
		DBCollection dbCollection = mongoDBClient.getCollection(MongoConstants.TABLE_NAME_ADIMAGE);
        DBObject dbObject = new BasicDBObject("url", url);
        DBObject updateData = new BasicDBObject();
        updateData.put("$set", dbObject);
        Date dateUpdateTime = new Date();
        dbObject.put("dataUpdateTime", dateUpdateTime);
        dbCollection.update(new BasicDBObject("md5", md5), updateData, true, false);
	}

}
